package com.sky.zzh.today.date;

import com.sky.zzh.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * This is a more complex implementation of the TimeView consisting of a LinearLayout with two
 * TimeViews. This allows primary text and sub-text, such as the name of the day and the day of the
 * month. This class expects the text that it is passed via {@link #setVals(TimeObject)} or
 * {@link #setVals(TimeView)} to contian the primary string followed by a space and then the
 * secondary string.
 */
public class TimeLayoutView extends LinearLayout implements TimeView {

  private Context mContext;
  protected long endTime, startTime;
  protected String text;
  protected boolean isCenter = false, isOutOfBounds = false;

  protected TextView bottomView;
  protected TextView topView;

  /**
   * constructor
   *
   * @param isCenterView true if the element is the centered view in the ScrollLayout
   * @param topTextSize text size of the top TextView in dps
   * @param bottomTextSize text size of the bottom TextView in dps
   * @param lineHeight LineHeight of the top TextView
   */
  public TimeLayoutView(Context context, boolean isCenterView, int topTextSize, int bottomTextSize,
      float lineHeight) {
    super(context);
    mContext = context;
    setupView(context, isCenterView, topTextSize, bottomTextSize, lineHeight);
  }

  /**
   * 产生每天的view
   *
   * Setting up the top TextView and bottom TextVew
   *
   * @param isCenterView true if the element is the centered view in the ScrollLayout
   * @param topTextSize text size of the top TextView in dps
   * @param bottomTextSize text size of the bottom TextView in dps
   * @param lineHeight LineHeight of the top TextView
   */
  protected void setupView(Context context, boolean isCenterView, int topTextSize,
      int bottomTextSize, float lineHeight) {
    setOrientation(VERTICAL);
    setGravity(Gravity.CENTER);
    bottomView = new TextView(context);
    bottomView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
    bottomView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, bottomTextSize);

    topView = new TextView(context);
    topView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
    topView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, topTextSize);

    bottomView.setLineSpacing(0, lineHeight);

    // 设置字体的颜色

    if (isCenterView) {
      isCenter = true;
      bottomView.setTextColor(Color.parseColor("#1cd98e"));
      topView.setTypeface(Typeface.DEFAULT_BOLD);
      topView.setTextColor(Color.parseColor("#1cd98e"));
      // bottomView.setPadding(0, 5 - (int) (topTextSize / 15.0), 0, 0);
      topView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.boradunderline));
    } else {
      // bottomView.setPadding(0, 5, 0, 0);
      bottomView.setTextColor(Color.parseColor("#0e9cd3"));
      topView.setTextColor(Color.parseColor("#0e9cd3"));
    }

    addView(bottomView);
    addView(topView);

  }


  public void setVals(TimeObject to) {
    text = to.text.toString();
    setText();
    this.startTime = to.startTime;
    this.endTime = to.endTime;
  }


  public void setVals(TimeView other) {
    text = other.getTimeText().toString();
    setText();
    startTime = other.getStartTime();
    endTime = other.getEndTime();
  }

  /**
   * sets the TextView texts by splitting the text into two
   */
  protected void setText() {
    String[] splitTime = text.split(" ");
    topView.setText(splitTime[0]);
    char[] charArray = splitTime[1].toCharArray();
    bottomView.setText(String.valueOf(charArray[1]));
  }


  public String getTimeText() {
    return text;
  }


  public long getStartTime() {
    return startTime;
  }


  public long getEndTime() {
    return endTime;
  }

  public boolean isOutOfBounds() {
    return isOutOfBounds;
  }

  public void setOutOfBounds(boolean outOfBounds) {
    if (outOfBounds && !isOutOfBounds) {
      bottomView.setTextColor(0x44666666);
      topView.setTextColor(0x44666666);
    } else if (!outOfBounds && isOutOfBounds) {
      bottomView.setTextColor(0xFF666666);
      topView.setTextColor(0xFF666666);
    }
    isOutOfBounds = outOfBounds;
  }

}
